package com.milo.java8.date;

import com.milo.java8.optional.entity.Inner;
import com.milo.java8.optional.entity.Nested;
import com.milo.java8.optional.entity.Outer;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Optional;
import java.util.function.Supplier;

import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * 测试java8时间
 * @author milogenius
 * @date 2020/10/15 11:13
 *
 */
@Slf4j
public class DateTest {

    /**
     * 在Java 8中获取当天的日期
     */
    @Test
    public void test1() {
        LocalDate today = LocalDate.now();
        log.info("当前 Local date : " + today);
    }

    /**
     * 如何在Java 8中获取当前的年月日
     */
    @Test
    public void test2() {
        LocalDate today = LocalDate.now();
        int year = today.getYear();
        int month = today.getMonthValue();
        int day = today.getDayOfMonth();
        log.info("年 : {} 月 : {} 日 : {} \n", year, month, day);
        DayOfWeek dayOfWeek = today.getDayOfWeek();
        int i = today.lengthOfMonth();
        boolean leapYear = today.isLeapYear();
        log.info("星期几 :{} 当月有多少天 : {} 是不是闰年 : {}",dayOfWeek,i,leapYear);

    }

    /**
     * 在Java 8中如何获取某个特定的日期
     *
     */
    @Test
    public void test5() {

        LocalDate dateOfBirth = LocalDate.of(2010, 01, 14);

        System.out.println("Your Date of birth is : " + dateOfBirth);

    }

    /**
     * 在Java 8中如何检查两个日期是否相等
     */
    @Test
    public void test3() {
        LocalDate today = LocalDate.now();
        LocalDate date1 = LocalDate.of(2014, 01, 14);
        if (date1.equals(today)) {

            System.out.printf("Today %s and date1 %s are same date %n", today, date1);

        }
    }

    /**
     *在Java 8中如何检查重复事件，比如说生日
     */
    @Test
    public void test6() {
        LocalDate today = LocalDate.now();
        LocalDate dateOfBirth = LocalDate.of(2010, 01, 14);
        MonthDay birthday = MonthDay.of(dateOfBirth.getMonth(), dateOfBirth.getDayOfMonth());
        MonthDay currentMonthDay = MonthDay.from(today);
        if (currentMonthDay.equals(birthday)) {
            System.out.println("Many Many happy returns of the day !!");
        } else {
            System.out.println("Sorry, today is not your birthday");
        }
    }

    /**
     * 如何在Java 8中获取当前时间
     */
    @Test
    public void test4(){
        LocalTime time = LocalTime.now();
        System.out.println("local time now : " + time);
    }

    /**
     *  如何增加时间里面的小时数
     */
    @Test
    public void test7(){
        LocalTime time = LocalTime.now();
        LocalTime newTime = time.plusHours(2); // adding two hours
        System.out.println("Time after 2 hours : " + newTime);
    }

    /**
     *  如何获取1周后的日期
     */
    @Test
    public void test8(){
        LocalDate today = LocalDate.now();
        LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
        System.out.println("Today is : " + today);
        System.out.println("Date after 1 week : " + nextWeek);
    }
    /**
     *  一年前后的日期
     */
    @Test
    public void test9(){
        LocalDate today = LocalDate.now();
        LocalDate previousYear = today.minus(1, ChronoUnit.YEARS);
        System.out.println("Date before 1 year : " + previousYear);
        LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
        System.out.println("Date after 1 year : " + nextYear);
    }
    /**
     *  在Java 8中使用时钟
     */
    @Test
    public void test10(){
        // Returns the current time based on your system clock and set to UTC.
        Clock clock = Clock.systemUTC();
        System.out.println("Clock : " + clock);
        // Returns time based on system clock zone Clock defaultClock =
        Clock.systemDefaultZone();
        System.out.println("Clock : " + clock);
    }
    /**
     *  在Java中如何判断某个日期是在另一个日期的前面还是后面
     */
    @Test
    public void test11(){
        LocalDate today = LocalDate.now();
        LocalDate tomorrow = LocalDate.of(2020, 10, 16);
        if(tomorrow.isAfter(today)){
            System.out.println("Tomorrow comes after today");
        }
        LocalDate yesterday = today.minus(1, ChronoUnit.DAYS);
        if(yesterday.isBefore(today)){
            System.out.println("Yesterday is day before today");
        }
    }
    /**
     *  在Java 8中处理不同的时区
     */
    @Test
    public void test12(){
        // Date and time with timezone in Java 8 ZoneId america = ZoneId.of("America/New_York");
        LocalDateTime localtDateAndTime = LocalDateTime.now();
       // ZonedDateTime dateAndTimeInNewYork = ZonedDateTime.of(localtDateAndTime, Aamerica );
       // System.out.println("Current date and time in a particular timezone : " + dateAndTimeInNewYork);
    }
    /**
     *  如何表示固定的日期，比如信用卡过期时间
     */
    @Test
    public void test13(){
        YearMonth currentYearMonth = YearMonth.now();
        System.out.printf("Days in month year %s: %d%n", currentYearMonth, currentYearMonth.lengthOfMonth());
        YearMonth creditCardExpiry = YearMonth.of(2020, Month.DECEMBER);
        System.out.printf("Your credit card expires on %s %n", creditCardExpiry);
    }

    /**
     * 如何在Java 8中检查闰年
     */
    @Test
    public void test14(){
        LocalDate today = LocalDate.now();
        if(today.isLeapYear()){
            System.out.println("This year is Leap year");
        }else {
            System.out.println("2014 is not a Leap year");

        }
    }

    /**
     * 两个日期之间包含多少天，多少个月
     */
    @Test
    public void test15(){
        LocalDate today = LocalDate.now();
        LocalDate java8Release = LocalDate.of(2020, Month.DECEMBER, 14);
        Period periodToNextJavaRelease = Period.between(today, java8Release);
        System.out.println("Months left between today and Java 8 release : " + periodToNextJavaRelease.getMonths() );
    }

    /**
     * 带时区偏移量的日期与时间
     */
    @Test
    public void test16(){
        LocalDateTime datetime = LocalDateTime.of(2014, Month.JANUARY, 14, 19, 30);
        ZoneOffset offset = ZoneOffset.of("+05:30");
        OffsetDateTime date = OffsetDateTime.of(datetime, offset);
        System.out.println("Date and Time with timezone offset in Java : " + date);
    }

    /**
     * 在Java 8中如何获取当前时间戳
     */
    @Test
    public void test17(){
        Instant timestamp = Instant.now();
        System.out.println("What is value of this instant " + timestamp);
    }

    /**
     * 如何在Java 8中使用预定义的格式器来对日期进行解析/格式化
     */
    @Test
    public void test18(){
        String dayAfterTommorrow = "20140116";
        LocalDate formatted = LocalDate.parse(dayAfterTommorrow, DateTimeFormatter.BASIC_ISO_DATE);
        System.out.printf("Date generated from String %s is %s %n", dayAfterTommorrow, formatted);
    }

    /**
     * 如何在Java中使用自定义的格式器来解析日期
     */
    @Test
    public void test19(){
        String goodFriday = "Apr 18 2014";
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd yyyy");
            LocalDate holiday = LocalDate.parse(goodFriday, formatter);
            System.out.printf("Successfully parsed String %s, date is %s%n", goodFriday, holiday);
        } catch (DateTimeParseException ex) {
            System.out.printf("%s is not parsable!%n", goodFriday);
            ex.printStackTrace();
        }
    }

    /**
     * 如何在Java 8中对日期进行格式化，转换成字符串
     */
    @Test
    public void test20(){
        LocalDateTime arrivalDate = LocalDateTime.now();
        try {
            DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm a");
            String landing = arrivalDate.format(format);
            System.out.printf("Arriving at : %s %n", landing);
        } catch (DateTimeException ex) {
            System.out.printf("%s can't be formatted!%n", arrivalDate);
            ex.printStackTrace();

        }
    }
}
